﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Win32;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Threading;
using System.Collections;

public partial class Teacher_TeacherDelete : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridViewDataBind();
        }
    }
    protected void GridViewDataBind()
    {
        DBaccess db = new DBaccess();
        DataSet ds = new DataSet();
        DataSet ds1 = new DataSet();
        ArrayList drarr = new ArrayList();
        ArrayList recoarr = new ArrayList();
        //DataSet ds2 = new DataSet();
        String getCid = HttpContext.Current.Request.QueryString["cid"];
        int i;
        ds = db.GetDataSet("select rid,logo,rname,keyword,score from mr_resource");
        DataTable dt = new DataTable();
        dt.Columns.Add("rid");
        dt.Columns.Add("image");
        dt.Columns.Add("rname");
        dt.Columns.Add("keyword");
        dt.Columns.Add("score");
        dt.Columns.Add("information");
        dt.Columns.Add("下载");
        for (i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = ds.Tables[0].Rows[i]["rid"];
            dr[2] = ds.Tables[0].Rows[i]["rname"];
            dr[3] = ds.Tables[0].Rows[i]["keyword"];
            dr[4] = ds.Tables[0].Rows[i]["score"];
            dr[5] = "information";
            dr[1] = ResolveUrl(ds.Tables[0].Rows[i]["logo"].ToString());
            dt.Rows.Add(dr);
        }

        GridView1.DataSource = dt;
        GridView1.AllowPaging = true;
        GridView1.PageSize = 5;
        GridView1.DataBind();
        //GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
        //GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
        GridView1.Columns[1].ItemStyle.Width = 150;
        GridView1.Columns[3].ItemStyle.Width = 200;
        SqlDataReader last_logn_time = db.GetReader("select last_resource_time from mr_member where mid = '" + Session["mid"] + "'");
        if (last_logn_time.Read())
        {
            string lasttime = last_logn_time.GetValue(0).ToString();
            SqlDataReader count = db.GetReader("select count(*) from mr_resource where upload_time >'" + lasttime + "'");
            if (count.Read())
            {
                HyperLink24.Text = "最新资源（" + count.GetValue(0).ToString() + "）";
            }
            else
                HyperLink24.Text = "最新资源（0）";
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridViewDataBind();
    }
    public void lbtn1_Click(object sender, EventArgs e)
    {
        try
        {
            System.Web.UI.WebControls.LinkButton t = (System.Web.UI.WebControls.LinkButton)sender;
            GridViewRow drv = (GridViewRow)t.NamingContainer;
            int rowIndex = drv.RowIndex;
            string text;
            text = GridView1.Rows[rowIndex].Cells[1].Text.ToString();
            DBaccess db = new DBaccess();
            int deleteview = db.ExecSql("delete from mr_resource where rid = '" + text + "'");
            SqlDataReader read = db.GetReader("select resource from mr_resource where rid='" + text + "'");
            read.Read();
            string path = (string)read.GetValue(0);
            string fullpath = MapPath(path);
            File.Delete(fullpath);
        }
        catch (Exception ex)
        {

            //MessageBox.Show("Error: Could not read file from db. Original error: " + ex.Message);
        }
    }
   
    protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "download")
        {
            //GridViewRow row  =   null ;
            //Control cmdControl  =  e.CommandSource  as  Control;
            //row  =  cmdControl.NamingContainer  as  GridViewRow;
            //int id = Convert.ToInt32(GridView1.Rows[row.RowIndex].Cells[0].Text);
            //string text;
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow selectedRow = GridView1.Rows[index];
            TableCell contactMid = selectedRow.Cells[0];
            string contact = contactMid.Text;
            DBaccess db = new DBaccess();
            SqlDataReader path = db.GetReader("select resource from mr_resource where rid = '" + contact + "'");
            path.Read();
            string filename = System.IO.Path.GetFileName((string)path.GetValue(0));
            Response.Clear();
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
            Response.WriteFile((string)path.GetValue(0));
            Response.Flush();
            Response.Close();
            string time = DateTime.Now.ToString();
            string sql = "insert into mr_download(sid,rid,time) values('" + Session["mid"].ToString() + "','" + contact + "','" + time + "')";
            db.ExecSql(sql);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string search = TextBox1.Text;
        DBaccess db = new DBaccess();

        DataTable dt = new DataTable();
        DataSet ds1 = new DataSet();
        ArrayList drarr = new ArrayList();
        ArrayList recoarr = new ArrayList();
        //DataSet ds2 = new DataSet();
        String getCid = HttpContext.Current.Request.QueryString["cid"];
        int i;
        DataSet ds = db.GetDataSet("select rid, rname, logo,keyword,score from mr_resource where rname like'%" + search + "%' or cid in (select cid from mr_course where cname like '%" + search + "%') or owner in (select mid from mr_member where mname like '%" + search + "%')");
        dt.Columns.Add("rid");
        dt.Columns.Add("image");
        dt.Columns.Add("rname");
        dt.Columns.Add("keyword");
        dt.Columns.Add("score");
        dt.Columns.Add("information");
        dt.Columns.Add("删除");
        for (i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = ds.Tables[0].Rows[i]["rid"];
            dr[2] = ds.Tables[0].Rows[i]["rname"];
            dr[3] = ds.Tables[0].Rows[i]["keyword"];
            dr[4] = ds.Tables[0].Rows[i]["score"];
            dr[5] = "information";
            dr[1] = ResolveUrl(ds.Tables[0].Rows[i]["logo"].ToString());
            dt.Rows.Add(dr);
        }

        GridView1.DataSource = dt;
        GridView1.DataBind();
        //GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
        //GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
        GridView1.Columns[1].ItemStyle.Width = 150;
        GridView1.Columns[3].ItemStyle.Width = 200;
        SqlDataReader last_logn_time = db.GetReader("select last_resource_time from mr_member where mid = '" + Session["mid"] + "'");
        if (last_logn_time.Read())
        {
            string lasttime = last_logn_time.GetValue(0).ToString();
            SqlDataReader count = db.GetReader("select count(*) from mr_resource where upload_time >'" + lasttime + "'");
            if (count.Read())
            {
                HyperLink24.Text = "最新资源（" + count.GetValue(0).ToString() + "）";
            }
            else
                HyperLink24.Text = "最新资源（0）";
        }
    }
}